package com.situ.mall.collection;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileFilter;

import java.util.*;

public class SetDemo {

    @Test
    public void test551() {
        // 创建HashSet对象
        Set<String> set = new HashSet<String>();
        // 给集合中添加自定义对象
        set.add("zhangsan");
        set.add("lisi");
        set.add("wangwu");
        set.add("zhangsan");
        set.add(new String("lisi"));
        for (String string : set) {
            System.out.println(string);
        }
    }

    @Test
    public void test34() {
        Set<Student> set = new HashSet<>();
        Student student1 = new Student(1, "zhangsan");
        Student student2 = new Student(2, "lisi");
        Student student3 = new Student(3, "wangwu");
        Student student4 = new Student(1, "zhangsan");
        set.add(student1);
        set.add(student2);
        set.add(student3);
        set.add(student3);
        set.add(student4);
        for (Student student : set) {
            System.out.println(student);
        }
    }


    @Test
    public void test4() {
        Set<String> set = new LinkedHashSet<String>();
        //Set<String> set = new HashSet<String>();
        set.add("bbb");
        set.add("aaa");
        set.add("abc");
        set.add("bbc");
        Iterator it = set.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }

    @Test
    public void test5() {
        TreeSet<Student> treeSet = new TreeSet<>();
        treeSet.add(new Student(1, "张三", 23));
        treeSet.add(new Student(2, "李四", 13));
        treeSet.add(new Student(3, "周七", 13));
        treeSet.add(new Student(4, "王五", 43));
        treeSet.add(new Student(5, "赵六", 33));

        System.out.println(treeSet);
    }

    @Test
    public void test22() {
        File currentDirectory = new File("D:\\Java2403\\mall");
        //File在Java里面既可以表示文件也可以表示文件夹
        //File[] files = currentDirectory.listFiles();
        //过滤掉目录里面隐藏的文件，这些隐藏的文件“.”开头
        File[] files = currentDirectory.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return !pathname.getName().startsWith(".");
            }
        });
        Collections.sort(Arrays.asList(files), new Comparator<File>() {
            @Override
            public int compare(File file1, File file2) {
                if (file1.isDirectory() && file2.isFile()) {
                    return 1;
                } else if (file1.isFile() && file2.isDirectory()) {
                    return -1;
                } else {
                    //file1和file2要么同时是文件夹要么同时是文件
                    return file1.getName().compareTo(file2.getName());
                }
            }
        });

        for (File file : files) {
            System.out.println(file.getName());
        }
    }
}
